﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SkladLib
{
    /// <summary>
    /// Interaction logic for CenyProfily.xaml
    /// </summary>
    public partial class CenyProfily : Window
    {
        public delegate void ValueChangedEventHandler(object sender);
        public event ValueChangedEventHandler ChangedRows;
        List<userRow> rows = new List<userRow>();
        List<string> cenyProfily =new List<string>();
        public CenyProfily()
        {
            InitializeComponent();
        }

        private void btnPridat_Click(object sender, RoutedEventArgs e)
        {
            userRow row = new userRow();
            row.Deleted+=row_Deleted;
            stpanRows.Children.Insert(stpanRows.Children.Count - 1, row);
        //    stpanRows.Children.Add(row);
        }

        void row_Deleted(object sender, string prefix)
        {
            DataLib.Settings.databases.dbCeny.DeleteRow("Ceny", "prefix", prefix);
            for(int i = 0; i< stpanRows.Children.Count;i++)
            {
                if(stpanRows.Children[i].GetHashCode() == sender.GetHashCode())
                {
                    stpanRows.Children.RemoveAt(i);
                    break;
                }
            }
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
             
            List<string> cenyProfily = DataLib.Settings.databases.dbCeny.QueryGetListString("SELECT prefix FROM Ceny");
            foreach (string profil in cenyProfily)
            {
                userRow row = new userRow(profil);
                row.Deleted += row_Deleted;
                stpanRows.Children.Insert(stpanRows.Children.Count-1, row);
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            bool isRowsValid = true;
            for (int i = 0; i < stpanRows.Children.Count - 1; i++)                             //Kontrola validity u6ivatelského rozhraní
            {
                if (!((userRow)stpanRows.Children[i]).isValid())
                {
                    isRowsValid = false;
                    break;
                }
            }

            if (isRowsValid)                                                    //Přidání do DB
            {
                for (int i = 0; i < stpanRows.Children.Count - 1;i++ )
                {
                    AddRowToDB((userRow)stpanRows.Children[i]);
                }
                if (ChangedRows != null)
                    ChangedRows(this);

                this.Close();
            }
        }

        private void AddRowToDB(userRow row)
        {
            Dictionary<string, string> data = row.ToDictionary();
            if (row.isNew)
                DataLib.Settings.databases.dbCeny.AddRow("Ceny", data);               //Nahrání nového řádku
            else
                DataLib.Settings.databases.dbCeny.UpdateRow(data, "Ceny", "WHERE prefix='" + row.prefix + "'");        //Update řádku
        }
    }
}
